package cn.edu.hatu.bookshop.controller;

import cn.edu.hatu.bookshop.entity.Book;
import cn.edu.hatu.bookshop.entity.Cart;
import cn.edu.hatu.bookshop.entity.Customer;
import cn.edu.hatu.bookshop.service.BookService;
import cn.edu.hatu.bookshop.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class CartController {
    private CartService cartService;
    private BookService bookService;
    @Autowired
    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    @Autowired
    public void setCartService(CartService cartService) {
        this.cartService = cartService;
    }
    @RequestMapping("/addToCart")
    @Transactional
    public String addToCart(Model model,HttpServletRequest request){
        int customerId =0;
        String userName ="";
        int bookId = Integer.parseInt(request.getParameter("bookId"));
        int count = Integer.parseInt(request.getParameter("count"));
        Book book = bookService.findById(bookId);
        double price = book.getPrice();
        String name = book.getBookName();
        System.out.println("bookId:"+bookId);
        System.out.println("count:"+count);
        //System.out.println("price:"+price);
        Cookie[] cookies = request.getCookies();//获取cookie数组
        for (Cookie c:cookies   //遍历cookie
        ) {
            if (c.getName().equals("customerId")) {
                customerId = Integer.parseInt(c.getValue());
                break;
            }
        }
        Cart cart = new Cart(customerId,bookId,count,price,name);
        cartService.addCart(cart);
        return "redirect:myCart";
    }
    @RequestMapping("/toBuy")
    @Transactional
    public String toBuy(Model model,HttpServletRequest request){
        int cartId = Integer.parseInt(request.getParameter("cartId"));
        Cart cart = cartService.findByCartId(cartId);
        model.addAttribute("cart",cart);
        double totalPrice = cart.getPrice()*cart.getCount();
        model.addAttribute("totalPrice",totalPrice);
        cartService.delByCartId(cartId);
        return "Customer/BuyBook";
    }
    @RequestMapping("/directBuy")
    @Transactional
    public String directBuy(HttpServletRequest request,Model model){
        int bookId = Integer.parseInt(request.getParameter("bookId"));
        int count = Integer.parseInt(request.getParameter("count"));
        Book book = bookService.findById(bookId);
        double price = book.getPrice();
        String name = book.getBookName();
        Cart cart = new Cart(1,bookId,count,price,name);
        model.addAttribute("cart",cart);
        double totalPrice = cart.getPrice()*cart.getCount();
        model.addAttribute("totalPrice",totalPrice);
        return "Customer/BuyBook";
    }
    @RequestMapping("/myCart")
    @Transactional
    public String myCart(Model model,HttpServletRequest request){
        int customerId = 0;
        Cookie[] cookies = request.getCookies();//获取cookie数组
        List<Cart> carts = new ArrayList<Cart>();
        String message = "";
        for (Cookie c:cookies   //遍历cookie
        ) {
            if (c.getName().equals("customerId")) {
                customerId = Integer.parseInt(c.getValue());
                carts = cartService.findByCustomerId(customerId);
                break;
            }
        }
        if(carts.size()==0){
            message="您的购物车为空，快去选购心仪商品吧！";
        }
        if(customerId==0){
            message="您还没有登录，请先登录！";
        }
        System.out.println("customerId"+customerId);


        model.addAttribute("message",message);
        model.addAttribute("carts",carts);
        return "Customer/MyCart";
    }

}
